package com.zytlifeservice.controller.pay;

import com.qianmi.open.api.ApiException;
import com.zytlifeservice.domain.Result;
import com.zytlifeservice.exception.UserException;
import com.zytlifeservice.service.pay.WiredTvRechargeOrderService;
import com.zytlifeservice.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 有线电视控制类
 * Created by Administrator on 2017/6/3.
 */
@RestController
@RequestMapping(value = "/wiredTv")
public class WiredTvRechargeController {
    @Autowired
    private WiredTvRechargeOrderService wiredTvRechargeOrderService;//有线电视业务对象

    @PostMapping(value = "/tvList")
    public Result tvList(@RequestParam("address") String city) throws Exception {
        return wiredTvRechargeOrderService.sendTv(city);
    }

    //有线电视下单
    @PostMapping(value = "/preCreate")
    public Result preCreate(@RequestParam("address") String address,
                            @RequestParam("rechargeAccount") String rechargeAccount,
                            @RequestParam("itemId") String itemId,
                            @RequestParam("rechargeAmount") String rechargeAmount,
                            @RequestParam("itemName") String itemName,
                            @RequestParam("userId") Long userId) {
        System.out.println("进入有线电视下单方法");
        try {
            String[] result = wiredTvRechargeOrderService.createOrder(address, rechargeAmount, itemName, itemId, rechargeAccount, userId);
            if ("success".equals(result[0])) {
                return ResultUtil.success(result[1], "预下单成功");
            } else {
                throw new UserException(result[1], 202);
            }
        } catch (ApiException e) {
            e.printStackTrace();
            throw new UserException("预下单失败", 202);
        }
    }

    @PostMapping(value = "/wxPreCreate")
    public Result wxPreCreate(@RequestParam("address") String address,
                              @RequestParam("rechargeAccount") String rechargeAccount,
                              @RequestParam("itemId") String itemId,
                              @RequestParam("rechargeAmount") String rechargeAmount,
                              @RequestParam("itemName") String itemName,
                              @RequestParam("userId") Long userId,
                              @RequestParam("ip") String ip) {
        String[] result = wiredTvRechargeOrderService.wxCreateOrder(address, rechargeAmount, itemName, itemId, rechargeAccount, userId, ip);
        if ("success".equals(result[0])) {
            return ResultUtil.success(result[1], "预下单成功");
        } else {
            throw new UserException(result[1], 202);
        }
    }
}
